package edu.hubu.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import net.moon.jpa.model.BaseEntity;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Proxy;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

/**
 * @author moonlan
 * @date 2020/10/18 下午4:52
 */

@Entity
@Table(name = "announcement")
//逻辑删除注解，删除sql变成了update
@SQLDelete(sql = "update announcement set announcement_is_deleted = true where announcement_id = ?")
//where条件带上了逻辑删除条件
@Where(clause = "announcement_is_deleted = false")
@ApiModel("公告")
@JsonIgnoreProperties(value = {"hibernateLazyInitializer", "handler", "fieldHandler"})
@EntityListeners(AuditingEntityListener.class)
@Proxy(lazy = false)
public class Announcement extends BaseEntity implements Serializable {

//    private static final long serialVersionUID=-4451341239174094573L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "snowflakeId")
    @GenericGenerator(name = "snowflakeId", strategy = "edu.hubu.commons.SnowflakeId", parameters = {@org.hibernate.annotations.Parameter(name = "prefix", value = "ANNOUNCEMENT")})
    @Column(name = "announcement_id")
    @ApiModelProperty(value = "announcement的id", position = 1)
    private String announcementId;

    @ManyToOne(targetEntity = User.class, cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)//1
    @JoinColumn(name = "announcement_user_sid", referencedColumnName = "user_sid")
    @ApiModelProperty(value = "公告的作者sid", position = 2)
    private User user;

    @ManyToOne(targetEntity = AnnouncementType.class, cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)//1
    @JoinColumn(name = "announcement_type_id", referencedColumnName = "announcement_type_id")
    @ApiModelProperty(value = "公告的类型id", position = 3)
    private AnnouncementType announcementType;

    @Column(name = "announcement_brief")
    @ApiModelProperty(value = "公告的简介", position = 4)
    private String announcementBrief;

    @Column(name = "announcement_title")
    @ApiModelProperty(value = "公告标题", position = 5)
    private String announcementTitle;

    @Column(name = "announcement_content", length = 20000, columnDefinition = "TEXT")
    @ApiModelProperty(value = "公告的内容", position = 6)
    private String announcementContent;

    @Column(name = "announcement_public_date")
    @ApiModelProperty(value = "公告的发布日期", position = 7)
    @CreatedDate
    private Date announcementPublicDate;

    @Column(name = "announcement_is_forbidden")
    @ApiModelProperty(value = "公告的是否被禁用", position = 8)
    private boolean announcementIsForbidden = false;

    @Column(name = "announcement_is_deleted")
    @ApiModelProperty(value = "公告的是否被删除", position = 9)
    private boolean announcementIsDeleted = false;

    @Column(name = "announcement_hit")
    @ApiModelProperty(value = "公告的点击量", position = 10)
    private Integer announcementHit;

    public Announcement(String announcementId, User user, AnnouncementType announcementType, String announcementBrief, String announcementTitle, String announcementContent, Date announcementPublicDate, boolean announcementIsForbidden, boolean announcementIsDeleted, Integer announcementHit) {
        this.announcementId = announcementId;
        this.user = user;
        this.announcementType = announcementType;
        this.announcementBrief = announcementBrief;
        this.announcementTitle = announcementTitle;
        this.announcementContent = announcementContent;
        this.announcementPublicDate = announcementPublicDate;
        this.announcementIsForbidden = announcementIsForbidden;
        this.announcementIsDeleted = announcementIsDeleted;
        this.announcementHit = announcementHit;
    }

    public Announcement() {
    }

    public static AnnouncementBuilder builder() {
        return new AnnouncementBuilder();
    }

    public String getAnnouncementId() {
        return announcementId;
    }

    public Announcement setAnnouncementId(String announcementId) {
        this.announcementId = announcementId;
        return this;
    }

    public User getUser() {
        return user;
    }

    public Announcement setUser(User user) {
        this.user = user;
        return this;
    }

    public AnnouncementType getAnnouncementType() {
        return announcementType;
    }

    public Announcement setAnnouncementType(AnnouncementType announcementType) {
        this.announcementType = announcementType;
        return this;
    }

    public String getAnnouncementBrief() {
        return announcementBrief;
    }

    public Announcement setAnnouncementBrief(String announcementBrief) {
        this.announcementBrief = announcementBrief;
        return this;
    }

    public String getAnnouncementTitle() {
        return announcementTitle;
    }

    public Announcement setAnnouncementTitle(String announcementTitle) {
        this.announcementTitle = announcementTitle;
        return this;
    }

    public String getAnnouncementContent() {
        return announcementContent;
    }

    public Announcement setAnnouncementContent(String announcementContent) {
        this.announcementContent = announcementContent;
        return this;
    }

    public Date getAnnouncementPublicDate() {
        return announcementPublicDate;
    }

    public Announcement setAnnouncementPublicDate(Date announcementPublicDate) {
        this.announcementPublicDate = announcementPublicDate;
        return this;
    }

    public boolean isAnnouncementIsForbidden() {
        return announcementIsForbidden;
    }

    public Announcement setAnnouncementIsForbidden(boolean announcementIsForbidden) {
        this.announcementIsForbidden = announcementIsForbidden;
        return this;
    }

    public boolean isAnnouncementIsDeleted() {
        return announcementIsDeleted;
    }

    public Announcement setAnnouncementIsDeleted(boolean announcementIsDeleted) {
        this.announcementIsDeleted = announcementIsDeleted;
        return this;
    }

    public Integer getAnnouncementHit() {
        return announcementHit;
    }

    public Announcement setAnnouncementHit(Integer announcementHit) {
        this.announcementHit = announcementHit;
        return this;
    }

    public static class AnnouncementBuilder {
        private String announcementId;
        private User user;
        private AnnouncementType announcementType;
        private String announcementBrief;
        private String announcementTitle;
        private String announcementContent;
        private Date announcementPublicDate;
        private boolean announcementIsForbidden;
        private boolean announcementIsDeleted;
        private Integer announcementHit;

        AnnouncementBuilder() {
        }

        public AnnouncementBuilder announcementId(String announcementId) {
            this.announcementId = announcementId;
            return this;
        }

        public AnnouncementBuilder user(User user) {
            this.user = user;
            return this;
        }

        public AnnouncementBuilder announcementType(AnnouncementType announcementType) {
            this.announcementType = announcementType;
            return this;
        }

        public AnnouncementBuilder announcementBrief(String announcementBrief) {
            this.announcementBrief = announcementBrief;
            return this;
        }

        public AnnouncementBuilder announcementTitle(String announcementTitle) {
            this.announcementTitle = announcementTitle;
            return this;
        }

        public AnnouncementBuilder announcementContent(String announcementContent) {
            this.announcementContent = announcementContent;
            return this;
        }

        public AnnouncementBuilder announcementPublicDate(Date announcementPublicDate) {
            this.announcementPublicDate = announcementPublicDate;
            return this;
        }

        public AnnouncementBuilder announcementIsForbidden(boolean announcementIsForbidden) {
            this.announcementIsForbidden = announcementIsForbidden;
            return this;
        }

        public AnnouncementBuilder announcementIsDeleted(boolean announcementIsDeleted) {
            this.announcementIsDeleted = announcementIsDeleted;
            return this;
        }

        public AnnouncementBuilder announcementHit(Integer announcementHit) {
            this.announcementHit = announcementHit;
            return this;
        }

        public Announcement build() {
            return new Announcement(announcementId, user, announcementType, announcementBrief, announcementTitle, announcementContent, announcementPublicDate, announcementIsForbidden, announcementIsDeleted, announcementHit);
        }

        public String toString() {
            return "Announcement.AnnouncementBuilder(announcementId=" + this.announcementId + ", user=" + this.user + ", announcementType=" + this.announcementType + ", announcementBrief=" + this.announcementBrief + ", announcementTitle=" + this.announcementTitle + ", announcementContent=" + this.announcementContent + ", announcementPublicDate=" + this.announcementPublicDate + ", announcementIsForbidden=" + this.announcementIsForbidden + ", announcementIsDeleted=" + this.announcementIsDeleted + ", announcementHit=" + this.announcementHit + ")";
        }
    }
}
